<template>
    <div>
        <el-button type="primary" size="small" @click="addSource" style="margin-bottom: 20px">添加客户来源</el-button>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="name"
            label="来源名称">
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="添加时间">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="size" @click="edit(scope.row.id, scope.row.name)" size="small">编辑</el-button>
                <el-button type="size" @click="remove(scope.row.id, scope.row.name)" size="small">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
    </div>
</template>

<script>
  import {getSourceList, saveSourceAdmin, editSourceAdmin, deleteSourceAdmin} from '@/api/customer'
    export default {
        name: "source",
        data() {
            return{
                tableData:[]
            }
        },
        methods:{
            init() {
                getSourceList().then(res => {
                    this.tableData = res.data.data
                })
            },
            addSource() {
                this.$prompt('客户来源名称', '添加客户来源', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(({ value }) => {
                    saveSourceAdmin(value).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                        this.init()
                    })
                })
            },
            edit(id, name) {
                this.$prompt('客户来源名称', '修改客户来源', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputValue:name
                }).then(({ value }) => {
                    editSourceAdmin(id, value).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                        this.init()
                    })
                })
            },
            remove(id,name) {
                this.$confirm(`确定要删除${name}来源吗？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "success"
                }).then(() => {
                    deleteSourceAdmin(id).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                        this.init()
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>
